package com.meiqiongproperty.yiguanjia.db.utils;

import android.content.Context;

import com.meiqiongproperty.yiguanjia.db.dao.DefaultHouseDao;
import com.meiqiongproperty.yiguanjia.db.dao.UserInfoDao;
import com.meiqiongproperty.yiguanjia.httpbean.UserInfo;
import com.meiqiongproperty.yiguanjia.utils.LogUtil;
import com.meiqiongproperty.yiguanjia.utils.Utils;

import java.util.ArrayList;

/**
 * @version V1.0
 * @packge com.meiqiongproperty.yiguanjia.db.utils
 * @User ChenJing
 * @Date 2017/5/22
 */

public class DBUserUtils {
    /**
     * -----------------------用戶信息----------------------------
     */
    private static UserInfoDao mUserInfoDao;
    private static ArrayList<UserInfo.UserinfoBean> mUserList;
    private static UserInfo.UserinfoBean mUser;

    public static UserInfo.UserinfoBean getUserInfo(Context context) {
        mUserInfoDao = new UserInfoDao(context);
        mUserList = qyerySqlUser(context, mUserInfoDao);
        if (null != mUserList && mUserList.size() != 0) {
            return mUserList.get(0);
        }
        return null;
    }

    public static ArrayList<UserInfo.UserinfoBean> qyerySqlUser(Context context, UserInfoDao mUserInfoDao) {
        mUserList = mUserInfoDao.queryAll();
        return mUserList;
    }

    /**
     * 初始化UserInfoDao数据。
     */
    public static long initUserInfoDaoAndSetUser(Context context, UserInfo userinfo) {
        mUserInfoDao = new UserInfoDao(context);
        return mUserInfoDao.add(userinfo.getUserinfo());
    }
    public static int LoginOut(Context context){
        mUserInfoDao = new UserInfoDao(context);
        mDefaultHouseDao = new DefaultHouseDao(context);
       return mUserInfoDao.deleteAll()+mDefaultHouseDao.deleteAll();
    }

    public static boolean isLoginOut(Context context){
        mUserInfoDao = new UserInfoDao(context);
        mDefaultHouseDao = new DefaultHouseDao(context);
        mUserInfoDao.deleteAll();
        mDefaultHouseDao.deleteAll();
        if (Utils.getToken(context)!=null){
            return false;
        }
        return true;
    }


    /**
     * 判断是否登陆状态
     */
    public static boolean isLogin(Context context) {
        mUserInfoDao = new UserInfoDao(context);
        if (mUserInfoDao.queryAll().size() != 0) {
            LogUtil.d("登录了");
            return true;
            //mIsError.set(true);//已登录
        } else {
            LogUtil.d("没登录");
            return false;
            //mIsError.set(false);//未登陆
        }
    }


    /**
     * -----------------------Houses信息----------------------------
     */
    private static DefaultHouseDao mDefaultHouseDao;
    private static ArrayList<UserInfo.DefaultHouseBean> mDefaultHouseList;
    private static UserInfo.DefaultHouseBean mDefaultHouse;

    public static UserInfo.DefaultHouseBean getmDefaultHouse(Context context) {
        mDefaultHouseDao = new DefaultHouseDao(context);
        mDefaultHouseList = qyerySqlHouse(context, mDefaultHouseDao);
        if (null != mDefaultHouseList && mDefaultHouseList.size() != 0) {
            return mDefaultHouseList.get(0);
        }
        return null;
    }

    public static String getAddressEnd(Context context){
        mDefaultHouseDao = new DefaultHouseDao(context);
        mDefaultHouseList = qyerySqlHouse(context, mDefaultHouseDao);
        if (null != mDefaultHouseList && mDefaultHouseList.size() != 0) {
            return mDefaultHouseList.get(0).getArea()+mDefaultHouseList.get(0).getCommunityAddress()+mDefaultHouseList.get(0).getBuild()+mDefaultHouseList.get(0).getRoomArea();
        }
        return null;
    }

    public static String getAddress(Context context){
        mDefaultHouseDao = new DefaultHouseDao(context);
        mDefaultHouseList = qyerySqlHouse(context, mDefaultHouseDao);
        if (null != mDefaultHouseList && mDefaultHouseList.size() != 0) {
            return mDefaultHouseList.get(0).getCity()+mDefaultHouseList.get(0).getArea()+mDefaultHouseList.get(0).getCommunityAddress()+mDefaultHouseList.get(0).getBuild()+mDefaultHouseList.get(0).getRoomArea();
        }
        return null;
    }

    public static ArrayList<UserInfo.DefaultHouseBean> qyerySqlHouse(Context context, DefaultHouseDao mDefaultHouseDao) {
        mDefaultHouseList = mDefaultHouseDao.queryAll();
        return mDefaultHouseList;
    }

    /**
     * 初始化UserInfoDao数据。
     */
    public static long initHouseDao(Context context, UserInfo.DefaultHouseBean defaultHouseBean) {
        mDefaultHouseDao = new DefaultHouseDao(context);
        return mDefaultHouseDao.add(defaultHouseBean);
    }
}
